package dev.hitools.utils.kts

import android.content.res.ColorStateList
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.util.Log
import androidx.core.graphics.drawable.DrawableCompat
import dev.hitools.utils.kts.BitmapExt.toByte


object DrawableExt {
    /**
     * 附着颜色
     */
    fun Drawable.tint(color: ColorStateList?): Drawable {
        return if (color == null) {
            this
        } else {
            val drawable = DrawableCompat.wrap(this)
            DrawableCompat.setTintList(drawable, color)
            drawable
        }
    }

    /**
     * 转换成Bitmap
     */
    fun Drawable.toBitmap(): Bitmap {
        val w = intrinsicWidth
        val h = intrinsicHeight

        val config = Bitmap.Config.ARGB_8888
        val bitmap = Bitmap.createBitmap(w, h, config)
        val canvas = Canvas(bitmap)
        setBounds(0, 0, w, h)
        draw(canvas)
        return bitmap
    }

    /**
     * 转换成ByteArray
     */
    fun Drawable.toByte(): ByteArray {
        if (this !is BitmapDrawable) {
            Log.e("DrawableExt", "drawableToBytes:  not a bitmap drawable")
            return byteArrayOf()
        }
        return bitmap.toByte()
    }
}